let that
class Keybord {
	constructor(name, activeClass = 'active', errorClass = 'error') {
		that = this
		this.Keybord = document.querySelector(name)
		this.str = '1234567890ABCDEFGHIJKLMNOPQRSTUBWXVZ'
		this.KeybordCode = '' // 用户按下的键的值
		this.randomCode = 'W' // 随机生成的键的值
		this.erroCode = '' // 按错的键
		this.activeClass = activeClass // 激活类
		this.errorClass = errorClass // 错误类
		this.rightNum = 0 // 输入正确数量
		this.erroNum = this.timer = 0 // 输错数量 // 时间 计算打字速度
		this.total = 0 //累计的字符串数量
		this.startTime = new Date().getTime() // 开始时间
		this.endTime = 0
		this.result = {} // 存放结果
		this.getOne()
		this.init()
	}
	init() {
		document.addEventListener('keyup', this.handlKeyup)
	}
	// 处理键盘的弹起事件
	handlKeyup(e) {
		// 拿到键对应的值
		that.KeybordCode = String.fromCharCode(e.keyCode)
		// 回车为结束游戏
		that.endTime = new Date().getTime()
		if (e.keyCode == 13) {
			that.calculateRes(
				that.calculateTime(that.startTime, that.endTime),
				that.total,
				that.rightNum
			)
			alert(
				`打字速度为${that.result.spped}/分,一共练习了${that.total}次,打错${that.result.erroeNums},正确率${that.result.RightRat}% `
			)
			// 程序初始化 直接刷新页面
			location.reload()
			return
		}
		// 用户按下的按键是正确的
		if (that.randomCode === that.KeybordCode) {
			// 开启新的随机字符串
			that.rightNum++
			that.randomStr()
			that.calculateRes(
				that.calculateTime(that.startTime, that.endTime),
				that.total,
				that.rightNum
			)
		} else {
			that.erroCode = that.KeybordCode
			// 用户按错了
			that.erroNum++
			that.calculateRes(
				that.calculateTime(that.startTime, that.endTime),
				that.total,
				that.rightNum
			)
			// 找到按错的键 添加错误类名error
			that.removeALLClass(that.errorClass)
			that.addClass(that.errorClass)
		}
	}
	// 产生随机的字符串
	randomStr() {
		const index = this.getRandom(0, this.str.length - 1)
		const arr = Array.from(this.str)
		this.randomCode = arr[index]
		// 开启新的一轮 直接初始化程序 计数器加1
		this.total++
		this.getOne()
	}
	// 生成随机数
	getRandom(n, m) {
		return Math.floor(Math.random() * (m - n + 1) + n)
	}
	// 添加一个激活类
	getOne() {
		//	只需要拿到 左侧布局中的大写字母的键盘即可
		this.Keybord.children[0].childNodes.forEach((node) => {
			// 拿到所有的元素节点
			if (node.nodeType == 1) {
				const attributes = node.attributes
				Array.from(attributes).forEach((attr) => {
					if (attr.nodeName === 'data-txt') {
						// 如果键盘字母等于当前生成的字母 加上active类名
						if (attr.nodeValue === this.randomCode) {
							// 去除其他的active  和 所有的error类
							this.removeALLClass(this.activeClass)
							this.removeALLClass(this.errorClass)
							node.classList.add(this.activeClass)
						}
					}
				})
			}
		})
	}
	// 添加类名
	addClass(className) {
		console.log(className)
		this.Keybord.children[0].childNodes.forEach((node) => {
			// 拿到所有的元素节点
			if (node.nodeType == 1) {
				const attributes = node.attributes
				Array.from(attributes).forEach((attr) => {
					if (attr.nodeName === 'data-txt') {
						// 去除其他的active 类
						if (that.KeybordCode === attr.nodeValue) {
							node.classList.add(className)
						}
						if (that.erroCode + '' === attr.nodeValue) {
							node.classList.add(className)
						}
					}
				})
			}
		})
	}
	// 删除所有的类名 排他思想
	removeALLClass(className) {
		this.Keybord.children[0].childNodes.forEach((node) => {
			// 拿到所有的元素节点
			if (node.nodeType == 1) {
				const attributes = node.attributes
				Array.from(attributes).forEach((attr) => {
					if (attr.nodeName === 'data-txt') {
						// 去除class
						node.classList.remove(className)
					}
				})
			}
		})
	}
	// 统计打字速度
	calculateRes(timer, total, rightNum) {
		const spped = Math.ceil(total / timer)
		let RightRat = rightNum / (rightNum + that.erroNum)

		this.result.spped = spped
		this.result.RightRat = (Math.floor(RightRat * 100) / 100) * 100
		this.result.erroeNums = that.erroNum
	}
	// 计算事件差
	calculateTime(startTime, endTime) {
		const deff = endTime - startTime
		const M = Math.ceil(deff / 1000 / 60)
		return M
	}
}
